<?php 
class IndexController extends Core_Controller
{
    public function indexAction()
    {
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin/login');
        else 
        	$this->redirect('/admin/vm');
    }
    public function loginAction()
    {
        $isHTTPS = (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off')) ? true : false;
        $registry = $this->_registry;
        $loginHTTPS       = $registry['config']['https'];
        if($isHTTPS !== $loginHTTPS)
        {
        	$protocol = $isHTTPS ? 'http://' : 'https://';
        	$host     = $_SERVER['HTTP_HOST'];
        	$baseUrl  = $this->_baseUrl;
        	$uri      = $registry['http']->getRequestUri();
        	$url      = $protocol.$host.$baseUrl.'/'.$uri;
        	header("Location: $url");
        }
        
        $this->_view->title = "Đăng nhập";
        $this->_view->layout(false);
    	if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        $homepage = 'http://'.$_SERVER['HTTP_HOST'].$this->_baseUrl.'/admin';
        if(isset($_SESSION['admin_id'])) header("Location: $homepage");   
        
        //status = 1: khong co loi , 0: co loi
        $status = 1;
        if(isset($_POST['loginButton']))
        {
            $u = trim($_POST['username']);
            $p = trim($_POST['password']);
            if($u==''||$p=='')
            {
                    $status = 0;
            }
            else
            {
             
                	$modelAdmin = new Admin_Model_Admin();
                    $result    = $modelAdmin->login($u,$p);
                    if($result != null)
                    {
                        $id        = $result[0]['admin_id'];
                        $fullname  = $result[0]['fullname'];
                        $_SESSION['admin_id']  = $id;
                        $_SESSION['admin_fullname'] = $fullname;
                        header("Location: $homepage"); 
                    }
                    else
                        $status = 0;   
            }
            $this->_view->input_username = $u;
        }
        $this->_view->status = $status;
    }
    
    public function logoutAction()
    {
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        unset($_SESSION['admin_id']);
        unset($_SESSION['admin_fullname']);
        $this->redirect('/admin/login');
    }
    
    public function accountAction()
    {
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Quản lý tài khoản";
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        
        //status = 0: chua submit,     1: cap nhat thong tin that bai, 2: cap nhat thong tin thanh cong
        //         3: mat khau cu sai  4: doi mat khau that bai      , 5: doi mat khau thanh cong   
        $this->_view->status = 0;
        
        $id = $_SESSION['admin_id'];
        $modelAdmin = new Admin_Model_Admin();
        $result    = $modelAdmin->getAdmin($id);
        if($result == null) exit("Tài khoản không tồn tại");
        
        $this->_view->username = $result[0]['username'];
        $this->_view->fullname = $result[0]['fullname'];
        
        if(isset($_POST['updateInf']))
        {
            $fullname = trim($_POST['name']);
            $ok       = $modelAdmin->updateFullname($id,$fullname);
            if($ok)
            {
                $_SESSION['admin_fullname']  = $fullname;
                $this->_view->fullname = $fullname;
                $this->_view->status   = 2;
            }
            else
                $this->_view->status   = 1;
        }
        elseif(isset($_POST['changepass']))
        {
            $old_password = trim($_POST['old_password']);
            $password     = trim($_POST['password']);
            $this->_view->status = $modelAdmin->changePassword($id,$old_password,$password) + 3;
        }
    }
}